package com.techzhi.address_web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisConfig implements CommandLineRunner {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        try {
            // 测试Redis连接
            redisTemplate.opsForValue().set("test:connection", "OK");
            String result = (String) redisTemplate.opsForValue().get("test:connection");
            redisTemplate.delete("test:connection");
            
            if ("OK".equals(result)) {
                log.info("✅ Redis连接测试成功！缓存功能已启用");
            } else {
                log.warn("⚠️ Redis连接测试失败，缓存可能无法正常工作");
            }
        } catch (Exception e) {
            log.error("❌ Redis连接失败: {}", e.getMessage());
            log.error("请检查Redis配置: host={}, port={}, password={}", 
                redisTemplate.getConnectionFactory().getConnection().getClientName(),
                "检查application.yaml中的redis配置");
        }
    }
}
